CMAKE_MINIMUM_REQUIRED(VERSION 2.4.3 FATAL_ERROR)
PROJECT(GD)

SET(CMAKE_MODULE_PATH "${GD_SOURCE_DIR}/cmake/modules")

if (BUILD_TEST)
	ENABLE_TESTING()
endif(BUILD_TEST)

if (USE_EXT_GD)
	message("Using GD at: ${USE_EXT_GD}")
	INCLUDE_DIRECTORIES(BEFORE ${GD_INCLUDE_DIR})
	FIND_PACKAGE(GD)
	if (GD_FOUND)
		INCLUDE_DIRECTORIES(BEFORE ${GD_INCLUDE_DIR})
		LINK_DIRECTORIES(BEFORE ${GD_LIBRARIES})
		SET(GD_LIB ${GD_LIBRARIES})
		SET(GD_LIBS_DIR ${GD_LIBRARY})

		message("GD libs #: ${GD_LIBRARIES}")
		message("GD lib #: ${GD_LIBRARY}")
		message("GD include: ${GD_INCLUDE_DIR}")
	else (GD_FOUND)
		message("No gd found")
	endif (GD_FOUND)
else (USE_EXT_GD)
	SET(GD_VERSION_MAJOR "2")
	SET(GD_VERSION_MINOR "0x0")
	SET(GD_VERSION_PATCH "34")
	SET(GD_VERSION "${GD_VERSION_MAJOR}.${GD_VERSION_MINOR}.${GD_VERSION_PATCH}")
	SET(GD_VERSION_STRING "${GD_VERSION}")

	SET(GD_VERSION_INT "20234")

	SET(PACKAGE GD)
	SET(PACKAGE_NAME GD)

	SET(CMAKE_REQUIRED_INCLUDES "/usr/include" "/usr/local/include")

	message(STATUS "ft2build: ${CMAKE_REQUIRED_INCLUDES}")

	include(CheckIncludeFiles)
	include(CheckIncludeFile)


	include(AC_HEADER_STDC)
	include(CheckPrototypeExists)

	FIND_PACKAGE(PNG REQUIRED)
	FIND_PACKAGE(ZLIB)
	FIND_PACKAGE(JPEG)
	FIND_PACKAGE(PTHREAD)
	FIND_PACKAGE(Freetype)
	FIND_PACKAGE(XPM)
	FIND_PACKAGE(Fontconfig)

	if (FREETYPE_FOUND)
		INCLUDE_DIRECTORIES(${FREETYPE_INCLUDE_DIR})
		message(STATUS "ft2build: ${HAVE_FT2BUILD_H} ${FREETYPE_INCLUDE_DIR}")
		SET(HAVE_FT2BUILD_H 1)
		SET(HAVE_LIBFREETYPE 1)
	ENDIF(FREETYPE_FOUND)

	IF(ZLIB_FOUND)
		INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})
		SET(HAVE_LIBZ 1)
		SET(HAVE_LIBPNG_PNG_H 1)
	ENDIF(ZLIB_FOUND)

	IF(PNG_FOUND)
		INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})
		SET(HAVE_LIBPNG 1)
	ENDIF(PNG_FOUND)

	IF(XPM_FOUND)
		INCLUDE_DIRECTORIES(${XPM_INCLUDE_DIR})
		SET(HAVE_LIBXPM 1)
	ENDIF(XPM_FOUND)

	IF(JPEG_FOUND)
		INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})
		SET(HAVE_LIBJPEG 1)
	ENDIF(JPEG_FOUND)

	IF(FONTCONFIG_FOUND)
		INCLUDE_DIRECTORIES(${FONTCONFIG_INCLUDE_DIR})
		SET(HAVE_LIBFONTCONFIG 1)
	ENDIF(FONTCONFIG_FOUND)

	#FIND_PATH(HAVE_ERRNO_H errno.h)
	#FIND_PATH(HAVE_STRINGS_H strings.h)
	#FIND_PATH(HAVE_LIMITS_H limits.h )
	#SET(HAVE_ERRNO_H 1)
	#SET(HAVE_LIBM 1)
	#FIND_PATH(HAVE_MEMORY_H memory.h)

	#FIND_PATH(HAVE_STRING_H string.h)
	#FIND_PATH(HAVE_SYS_STAT_H 1)
	#FIND_PATH(HAVE_SYS_TYPES_H 1)
	#FIND_PATH(HAVE_UNISTD_H unistd.h)
	#FIND_PATH(STDC_HEADERS 1)
	#FIND_PATH(HAVE_INTTYPES_H 1)
	#FIND_PATH(HAVE_STDDEF_H stddef.h)
	#FIND_PATH(HAVE_STDINT_H 1)
	#FIND_PATH(HAVE_STDLIB_H stdlib.h)

	SET(HAVE_CONFIG_H 1)

	ADD_DEFINITIONS(-DHAVE_CONFIG_H)

	SET (LIBGD_SRC_FILES
		gd.c
		gdfx.c
		gd_security.c
		gd_gd.c
		gd_gd2.c
		gd_io.c
		gd_io_dp.c
		gd_gif_in.c
		gd_gif_out.c
		gd_io_file.c
		gd_io_ss.c
		gd_jpeg.c
		gd_png.c
		gd_ss.c
		gd_topal.c
		gd_wbmp.c
		gdcache.c
		gdfontg.c
		gdfontl.c
		gdfontmb.c
		gdfonts.c
		gdfontt.c
		gdft.c
		gdhelpers.c
		gdhelpers.h
		gdkanji.c
		gdtables.c
		gdxpm.c
		jisx0208.h
		wbmp.c
		wbmp.h
	)

	set(BUILD_SHARED_LIBS On)

	INCLUDE_DIRECTORIES(BEFORE ${GD_SOURCE_DIR}/)

	CONFIGURE_FILE(config.h.cmake config.h ESCAPE_QUOTES)

	add_library(gd ${LIBGD_SRC_FILES})
	target_link_libraries(gd ${FREETYPE_LIBRARIES} ${PNG_LIBRARIES} ${JPEG_LIBRARIES} ${XPM_LIBRARIES} ${FONTCONFIG_LIBRARIES})
	SET(GD_LIB gd)
endif (USE_EXT_GD)

add_subdirectory(tests)